package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CollarUseApplyAddDTO;
import com.oa.admin.dto.CollarUseApplyListDTO;
import com.oa.admin.entity.CollarUseApply;
import com.oa.admin.entity.CollarUseDetails;
import com.oa.admin.mapper.CollarUseApplyMapper;
import com.oa.admin.service.ICollarUseApplyService;
import com.oa.admin.service.ICollarUseDetailsService;
import com.oa.admin.vo.CollarUseApplyDetailsVO;
import com.oa.admin.vo.InventoryUserListVO;
import com.oa.core.PageResult;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollarUseApplyServiceImpl extends ServiceImpl<CollarUseApplyMapper, CollarUseApply> implements ICollarUseApplyService {

    @Autowired
    private CollarUseApplyMapper collarUseApplyMapper;

    @Autowired
    private ICollarUseDetailsService collarUseDetailsService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @CommonService
    @Override
    public PageResult<InventoryUserListVO> findInventoryUserList(CollarUseApplyListDTO collarUseApplyListDTO) {
        Page page = collarUseApplyListDTO.getPage();
        return new PageResult<>(collarUseApplyMapper.findInventoryUserList(page, collarUseApplyListDTO), page.getPages());
    }

    @CommonService
    @Override
    public PageResult<InventoryUserListVO> findInventoryUserListApply(CollarUseApplyListDTO collarUseApplyListDTO) {
        Page page = collarUseApplyListDTO.getPage();
        return new PageResult<>(collarUseApplyMapper.findInventoryUserListApply(page, collarUseApplyListDTO), page.getPages());
    }

    @CommonService
    @Override
    public CollarUseApplyDetailsVO findCollarUseApplyDetails(String id) {
        return collarUseApplyMapper.findCollarUseApplyDetails(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(String userId, CollarUseApplyAddDTO collarUseApplyAddDTO) throws DataIsEmptyException, BusinessException {
        CollarUseApply collarUseApply = new CollarUseApply();
        collarUseApply.setRemark(collarUseApplyAddDTO.getRemake());
        collarUseApply.setUserId(userId);
        if (insert(collarUseApply)) {
            List<CollarUseDetails> collarUseDetails = new ArrayList<>();
            collarUseApplyAddDTO.getCollarUseDetailsAddDTOS().forEach(d -> {
                CollarUseDetails collarUseDetail = new CollarUseDetails();
                BeanUtils.copyProperties(d, collarUseDetail);
                collarUseDetail.setCollarUseApplyId(collarUseApply.getId());
                collarUseDetails.add(collarUseDetail);
            });
            if (collarUseDetailsService.insertBatch(collarUseDetails)) {
                User user = userService.selectById(userId);

                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(collarUseApply.getId());
                applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.ITEMS_RECEIVING.getId());
                applyApprovalAddDTO.setApplyApprovalName(user.getName() + ProcessDefineEnum.ITEMS_RECEIVING.getText());
                applyApprovalAddDTO.setApprovalUserId(user.getId());
                return applyApprovalService.addApproval(applyApprovalAddDTO);

            }
        }
        throw new DataIsEmptyException("提交申请失败");
    }
}
